package com.ling.pl.security.dao;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.security.model.DefaultGroup;
import com.ling.pl.security.model.GroupMember;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author bo.wang
 */
@Component(DefaultGroupDao.BEAN_ID)
public class DefaultGroupDao extends BaseDefaultGroupDao {
    public List<DefaultGroup> loadUserGroups(String username) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT g FROM GroupMember gm," + DefaultGroup.class.getName() + " g where gm.groupId = g.id and gm.username=:username");
        values.put("username", username);
        return findBy(query, values);
    }


    public DaoPage loadGroups(Map params, Integer pageIndex,
                              Integer pageSize) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append(" from DefaultGroup g where 1=1 ");
        query.append(" order by g.createDate desc");
        return pageBy(query, values, pageIndex, pageSize);
    }


    public DaoPage loadGroupMembers(Map params, Integer pageIndex,
                                    Integer pageSize) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append(" from " + GroupMember.class.getName() + " gm where gm.groupId=:groupId ");
        Object value = params.get("type");
        if (value.equals("user")) {
            query.append(" and gm.username is not null");
        } else if (value.equals("org")) {
            query.append(" and gm.orgId is not null");
        } else if (value.equals("position")) {
            query.append(" and gm.positionId is not null");
        }
        value = params.get("groupId");
        AssertHelper.notEmpty_assert(value, "groupId不能为空");
        values.put("groupId", value);
        return pageBy(query, values, pageIndex, pageSize);
    }

    public List<GroupMember> loadGroupMembers(Map params) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append(" from " + GroupMember.class.getName() + " gm where gm.groupId=:groupId ");
        Object value = params.get("groupId");
        AssertHelper.notEmpty_assert(value, "groupId不能为空");
        values.put("groupId", value);

        value = params.get("type");
        if (value.equals("user")) {
            if (AssertHelper.notEmpty(params.get("username"))) {
                query.append(" and gm.username=:username");
                values.put("username", params.get("username"));
            } else {
                query.append(" and gm.username is not null");
            }
        } else if (value.equals("org")) {
            if (AssertHelper.notEmpty(params.get("orgId"))) {
                query.append(" and gm.orgId=:orgId");
                values.put("orgId", params.get("orgId"));
            } else {
                query.append(" and gm.orgId is not null");
            }
        } else if (value.equals("position")) {
            if (AssertHelper.notEmpty(params.get("positionId"))) {
                query.append(" and gm.positionId=:positionId");
                values.put("positionId", params.get("positionId"));
            } else {
                query.append(" and gm.positionId is not null");
            }
        }
        return findBy(query, values);
    }
}

